package com.zzl.alligator.management.system.service.impl;


import com.zzl.alligator.management.system.domain.SysDictData;
import com.zzl.alligator.management.system.mapper.SysDictDataMapper;
import com.zzl.alligator.management.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 字典数据表(SysDictData)服务层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "DICT_CACHE")
public class SysDictDataService implements ISysDictDataService {
    
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    
    /**
     * 通过ID查询单条数据
     *
     * @param dictCode 主键
     * @return 实例对象
     */
    @Override
    public SysDictData findSysDictDataById(Long dictCode) {
        return sysDictDataMapper.findSysDictDataById(dictCode);
    }

   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysDictData 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysDictData> findSysDictDataList(SysDictData sysDictData) {
        return sysDictDataMapper.findSysDictDataList(sysDictData);
    }

    /**
     * 新增数据
     *
     * @param sysDictData 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#sysDictData.dictType")
    public int insertSysDictData(SysDictData sysDictData) {
        return sysDictDataMapper.insertSysDictData(sysDictData);
    }

    /**
     * 修改数据
     *
     * @param sysDictData 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#sysDictData.dictType")
    public int updateSysDictData(SysDictData sysDictData) {
        return sysDictDataMapper.updateSysDictData(sysDictData);
    }

    /**
     * 通过主键删除数据
     *
     * @param dictCode 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#dictType")
    public int deleteById(String dictType, Long dictCode) {
        return sysDictDataMapper.deleteById(dictCode);
    }

    /**
     * 根据字典类型从查找字典
     *
     * @param dictType 字典类型
     * @return 结果
     */
    @Override
    @Cacheable( key = "#dictType")
    public List<SysDictData> findSysDictDataByType(String dictType) {
        return sysDictDataMapper.findSysDictDataByType(dictType );
    }
}